package com.example.consumptionrecords.exception;

import com.alibaba.fastjson.support.spring.FastJsonJsonView;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

/**
 * 孙敬佳
 * 2024 \ 02 \ 26
 */

public class ExceptionHandler implements HandlerExceptionResolver {
	@Override
	public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
		ModelAndView mv = new ModelAndView();
		FastJsonJsonView view = new FastJsonJsonView();
		Map<String, Object> attributes = new HashMap<String, Object>();
		if (ex instanceof UnauthenticatedException) {
			attributes.put("code", "1000001");
			attributes.put("msg", "token错误");
		} else if (ex instanceof UnauthorizedException) {
			attributes.put("code", "1000002");
			attributes.put("msg", "用户无权限");
		} else {
			attributes.put("code", "1000003");
			attributes.put("msg", ex.getMessage());
		}

		view.setAttributesMap(attributes);
		mv.setView(view);
		return mv;
	}
}
